/***************************************************************
 * Name:      FileSyncMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    JackyLian ()
 * Created:   2012-12-06
 * Copyright: JackyLian ()
 * License:
 **************************************************************/

#include "FileSyncMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(FileSyncFrame)
#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/icon.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

#include "difflist.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(FileSyncFrame)
const long FileSyncFrame::ID_BUTTON1 = wxNewId();
const long FileSyncFrame::ID_TEXTCTRL1 = wxNewId();
const long FileSyncFrame::ID_PANEL1 = wxNewId();
const long FileSyncFrame::ID_BUTTON3 = wxNewId();
const long FileSyncFrame::ID_CHECKBOX1 = wxNewId();
const long FileSyncFrame::ID_PANEL3 = wxNewId();
const long FileSyncFrame::ID_BUTTON2 = wxNewId();
const long FileSyncFrame::ID_TEXTCTRL2 = wxNewId();
const long FileSyncFrame::ID_PANEL2 = wxNewId();
const long FileSyncFrame::ID_GAUGE1 = wxNewId();
const long FileSyncFrame::ID_LISTCTRL1 = wxNewId();
const long FileSyncFrame::ID_TEXTCTRL3 = wxNewId();
//*)

DEFINE_EVENT_TYPE(wxEVT_SYNC_ONE)

BEGIN_EVENT_TABLE(FileSyncFrame,wxFrame)
    //(*EventTable(FileSyncFrame)
    //*)
    EVT_COMMAND(ID_LISTCTRL1, wxEVT_SYNC_ONE, FileSyncFrame::OnSyncOne)
END_EVENT_TABLE()

FileSyncFrame::FileSyncFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(FileSyncFrame)
    wxBoxSizer* BoxSizer4;
    wxBoxSizer* BoxSizer6;
    wxBoxSizer* BoxSizer5;
    wxBoxSizer* BoxSizer2;
    wxBoxSizer* BoxSizer1;
    wxBoxSizer* BoxSizer3;

    Create(parent, wxID_ANY, _("文件同步程序 v1.1 --By JackyLian"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    {
    	wxIcon FrameIcon;
    	FrameIcon.CopyFromBitmap(wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_NORMAL_FILE")),wxART_FRAME_ICON));
    	SetIcon(FrameIcon);
    }
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer4 = new wxBoxSizer(wxVERTICAL);
    m_srcDirBtn = new wxButton(Panel1, ID_BUTTON1, _("选择源文件夹（最新）"), wxDefaultPosition, wxSize(135,63), 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer4->Add(m_srcDirBtn, 1, wxALL|wxSHAPED|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    m_srcDirText = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxSize(181,23), wxTE_READONLY|wxTE_CENTRE, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    BoxSizer4->Add(m_srcDirText, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(BoxSizer4);
    BoxSizer4->Fit(Panel1);
    BoxSizer4->SetSizeHints(Panel1);
    BoxSizer2->Add(Panel1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    Panel3 = new wxPanel(this, ID_PANEL3, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL3"));
    BoxSizer6 = new wxBoxSizer(wxVERTICAL);
    m_syncBtn = new wxButton(Panel3, ID_BUTTON3, _("同步"), wxDefaultPosition, wxSize(66,62), 0, wxDefaultValidator, _T("ID_BUTTON3"));
    m_syncBtn->Disable();
    m_syncBtn->SetForegroundColour(wxColour(0,128,0));
    BoxSizer6->Add(m_syncBtn, 1, wxALL|wxSHAPED|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    m_deleteCheckBox = new wxCheckBox(Panel3, ID_CHECKBOX1, _("删除多余文件"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
    m_deleteCheckBox->SetValue(false);
    BoxSizer6->Add(m_deleteCheckBox, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel3->SetSizer(BoxSizer6);
    BoxSizer6->Fit(Panel3);
    BoxSizer6->SetSizeHints(Panel3);
    BoxSizer2->Add(Panel3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    Panel2 = new wxPanel(this, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL, _T("ID_PANEL2"));
    BoxSizer5 = new wxBoxSizer(wxVERTICAL);
    m_dstDirBtn = new wxButton(Panel2, ID_BUTTON2, _("选择目标文件夹"), wxDefaultPosition, wxSize(135,63), 0, wxDefaultValidator, _T("ID_BUTTON2"));
    m_dstDirBtn->Disable();
    BoxSizer5->Add(m_dstDirBtn, 1, wxALL|wxSHAPED|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    m_dstDirText = new wxTextCtrl(Panel2, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxSize(181,23), wxTE_READONLY|wxTE_CENTRE, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    BoxSizer5->Add(m_dstDirText, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel2->SetSizer(BoxSizer5);
    BoxSizer5->Fit(Panel2);
    BoxSizer5->SetSizeHints(Panel2);
    BoxSizer2->Add(Panel2, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    BoxSizer1->Add(BoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    m_processBar = new wxGauge(this, ID_GAUGE1, 100, wxDefaultPosition, wxSize(489,27), 0, wxDefaultValidator, _T("ID_GAUGE1"));
    BoxSizer1->Add(m_processBar, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    BoxSizer3 = new wxBoxSizer(wxVERTICAL);
    m_diffList = new FileDiffList(this, ID_LISTCTRL1, wxDefaultPosition, wxSize(489,235), wxLC_REPORT|wxLC_HRULES|wxLC_VRULES, wxDefaultValidator, _T("ID_LISTCTRL1"));
    BoxSizer3->Add(m_diffList, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    m_logText = new wxTextCtrl(this, ID_TEXTCTRL3, wxEmptyString, wxDefaultPosition, wxSize(492,72), wxTE_MULTILINE|wxTE_READONLY, wxDefaultValidator, _T("ID_TEXTCTRL3"));
    wxLog::SetActiveTarget(new wxLogTextCtrl(m_logText));
    BoxSizer3->Add(m_logText, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    BoxSizer1->Add(BoxSizer3, 3, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    SetSizer(BoxSizer1);
    m_dirDialog = new wxDirDialog(this, _("Select directory"), wxEmptyString, wxDD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxDirDialog"));
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&FileSyncFrame::OnSrcDirBtnClick1);
    Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&FileSyncFrame::OnSyncBtnClick);
    Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&FileSyncFrame::OnDstDirBtn);
    Connect(wxEVT_SIZE,(wxObjectEventFunction)&FileSyncFrame::OnResize);
    //*)
}

FileSyncFrame::~FileSyncFrame()
{
    //(*Destroy(FileSyncFrame)
    //*)
}

void FileSyncFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void FileSyncFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void FileSyncFrame::OnSrcDirBtnClick1(wxCommandEvent& event)
{
    if( m_dirDialog->ShowModal() == wxID_OK )
    {
        m_srcDirText->SetValue(m_dirDialog->GetPath());
        m_diffList->Reload(m_srcDirText->GetValue());
        m_dstDirBtn->Enable();
    }
}

void FileSyncFrame::OnDstDirBtn(wxCommandEvent& event)
{
    if( m_dirDialog->ShowModal() == wxID_OK )
    {
        m_dstDirText->SetValue( m_dirDialog->GetPath() );
        m_diffList->Reload(m_srcDirText->GetValue());
        m_diffList->LoadDst( m_dstDirText->GetValue() );
        m_syncBtn->Enable();
    }
}

void FileSyncFrame::OnResize(wxSizeEvent& event)
{
    m_diffList->Resize(GetSize());
    event.Skip();
}

void FileSyncFrame::OnSyncBtnClick(wxCommandEvent& event)
{
    m_processBar->SetRange(m_diffList->GetItemCount() + 1);
    m_processBar->SetValue(0);
    if(m_diffList->Sync(m_deleteCheckBox->GetValue()) != 0)
    {
        wxMessageBox(wxT("同步失败！"));
    }
}

void FileSyncFrame::OnSyncOne(wxCommandEvent& event)
{
    if(!event.GetString().empty()) wxLogMessage(wxT("%s"), event.GetString().c_str());
    m_processBar->SetValue(m_processBar->GetValue()+1);
    if(m_processBar->GetValue() == m_processBar->GetRange())
    {
        wxMessageBox(wxT("同步成功！"));
        m_diffList->Reload(m_srcDirText->GetValue());
        m_diffList->LoadDst(m_dstDirText->GetValue());
    }
}
